package org.cn.lennon.mybatis.executors.impl;

import java.sql.SQLException;

import org.cn.lennon.mybatis.config.MapperStatment;
import org.cn.lennon.mybatis.executors.impl.handler.PreparedStatmentHandler;
import org.cn.lennon.mybatis.transcation.Transaction;

public class SimpleExecutors extends BaseExecutors {

	public SimpleExecutors(Transaction transaction) {
		super(transaction);
	}

	public <T> T query(MapperStatment mapperStatemnt, Object queryObject) throws SQLException {
		return query(mapperStatemnt, new Object[] { queryObject });
	}

	public <T> T query(MapperStatment mapperStatemnt, Object[] queryObjects) throws SQLException {
		try {
			PreparedStatmentHandler preparedStatmentHandler = new PreparedStatmentHandler(mapperStatemnt);
			return preparedStatmentHandler.query(queryObjects);
		} finally {
			transaction.close();
		}

	}

	public int update(MapperStatment mapperStatemnt, Object[] queryObjects) throws SQLException {
		try {
			PreparedStatmentHandler preparedStatmentHandler = new PreparedStatmentHandler(mapperStatemnt);
			return preparedStatmentHandler.update(queryObjects);
		} finally {
			transaction.close();
		}
	}

	public int insert(MapperStatment mapperStatemnt, Object[] queryObjects) throws SQLException {
		try {
			PreparedStatmentHandler preparedStatmentHandler = new PreparedStatmentHandler(mapperStatemnt);
			return preparedStatmentHandler.insert(queryObjects);
		} finally {
			transaction.close();
		}
	}

	public int delete(MapperStatment mapperStatemnt, Object[] queryObjects) throws SQLException {
		try {
			PreparedStatmentHandler preparedStatmentHandler = new PreparedStatmentHandler(mapperStatemnt);
			return preparedStatmentHandler.delete(queryObjects);
		} finally {
			transaction.close();
		}
	}

}
